package com.asktgapp.user.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.asktgapp.JGTool.JGColor;
import com.asktgapp.JGTool.Utils;
import com.asktgapp.MyConstant;
import com.asktgapp.R;
import com.asktgapp.application.ApiUtil;
import com.asktgapp.application.Apiservice;
import com.asktgapp.architecture.retrofit.ApiException;
import com.asktgapp.architecture.retrofit.ApiResponseBody;
import com.asktgapp.architecture.rxjava.ExceptionHandle;
import com.asktgapp.base.BaseFragment;
import com.asktgapp.model.MyPublishVO;
import com.asktgapp.model.RentDetialVO;
import com.asktgapp.modulebase.common.adapter.BaseAdapter;
import com.asktgapp.modulebase.common.adapter.BaseViewHolder;
import com.asktgapp.modulebase.common.util.PreferencesUtil;
import com.asktgapp.modulebase.common.util.Util;
import com.asktgapp.user.activity.AskDetialActivity;
import com.asktgapp.user.activity.EditQiuzuActicity;
import com.asktgapp.user.activity.EditRentActicity;
import com.asktgapp.user.activity.EditUsedMachineActivity;
import com.asktgapp.user.activity.FreeAskAnswerDetailActivity;
import com.asktgapp.utils.ImageDisplayUtil;
import com.yanzhenjie.recyclerview.swipe.SwipeMenu;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuBridge;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuCreator;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuItem;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuItemClickListener;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;
import com.yanzhenjie.recyclerview.swipe.touch.OnItemMoveListener;
import com.yanzhenjie.recyclerview.swipe.touch.OnItemMovementListener;
import com.yanzhenjie.recyclerview.swipe.widget.DefaultItemDecoration;

import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.HashSet;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * time:2018/2/24
 * Create By ZhaoKai
 * Email: dczk@foxmail.com
 * Description:
 */

public class MyPublishItemFragment extends BaseFragment {
    private SwipeMenuRecyclerView mRecyclerView;
    private BaseAdapter mBaseAdapter;
    private int type;
    private static final int VIEW_TYPE_USED = 1;
    private static final int VIEW_TYPE_RENT = 2;
    private static final int VIEW_TYPE_QIUZU = 3;
    private static final int VIEW_TYPE_USED_CHECK = 4;
    private static final int VIEW_TYPE_HELP_CHECK = 5;
    private static final int VIEW_TYPE_ASK = 6;
    private Apiservice mApiservice;

    public static MyPublishItemFragment newInstance(int type) {
        MyPublishItemFragment fragment = new MyPublishItemFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment_my_publish_item, container, false);
        return super.onCreateView(inflater, viewGroup, savedInstanceState);
    }

    @Override
    protected void initView(View rootView) {
        Bundle bundle = getArguments();
        type = bundle.getInt("type", MyConstant.TYPE_ALL);
        mRecyclerView = rootView.findViewById(R.id.recycle);
        mRecyclerView.addItemDecoration(new DefaultItemDecoration(JGColor.APP_BACKGROUND));
//        mRecyclerView.setItemViewSwipeEnabled(true);
        /**
         * 菜单创建器，在Item要创建菜单的时候调用。
         */
        SwipeMenuCreator swipeMenuCreator = new SwipeMenuCreator() {
            @Override
            public void onCreateMenu(SwipeMenu swipeLeftMenu, SwipeMenu swipeRightMenu, int viewType) {
                int width = getResources().getDimensionPixelSize(R.dimen.view_height_80);

                // 1. MATCH_PARENT 自适应高度，保持和Item一样高;
                // 2. 指定具体的高，比如80;
                // 3. WRAP_CONTENT，自身高度，不推荐;
                int height = ViewGroup.LayoutParams.MATCH_PARENT;
                SwipeMenuItem deleteItem = new SwipeMenuItem(getActivity())
                        .setBackgroundColor(Color.parseColor("#ff5c5d"))
                        .setText("删除")
                        .setTextColor(Color.WHITE)
                        .setWidth(width)
                        .setHeight(height);
                swipeRightMenu.addMenuItem(deleteItem);// 添加菜单到右侧。
            }
        };

//        mRecyclerView.setOnItemMovementListener(mItemMovementListener);
//        mRecyclerView.setOnItemMoveListener(onItemMoveListener);

        SwipeMenuItemClickListener mMenuItemClickListener = new SwipeMenuItemClickListener() {
            @Override
            public void onItemClick(SwipeMenuBridge menuBridge) {
                menuBridge.closeMenu();
                int direction = menuBridge.getDirection(); // 左侧还是右侧菜单。
                final int adapterPosition = menuBridge.getAdapterPosition(); // RecyclerView的Item的position。
                int menuPosition = menuBridge.getPosition(); // 菜单在RecyclerView的Item中的Position。
//                final Object bean = mBaseAdapter.getData().get(adapterPosition);

                if (direction == SwipeMenuRecyclerView.RIGHT_DIRECTION) {
                    final int position = adapterPosition;
                    int viewtype = mBaseAdapter.getItemViewType(position);
                    //0:二手机 1:设备出租 2:设备求租 3:工程师
                    HashMap params = new HashMap();
                    params.put("userid", PreferencesUtil.getString(getActivity(), PreferencesUtil.USER_ID));
                    switch (viewtype) {
                        case VIEW_TYPE_USED:
                            MyPublishVO.TwoHardListBean bean = (MyPublishVO.TwoHardListBean) mBaseAdapter.getData().get(position);
                            params.put("id", bean.getIdX());
                            params.put("isDel", 1);
                            Call<ApiResponseBody> call = mApiservice.deleteUsedMachine(params);
                            call.enqueue(new Callback<ApiResponseBody>() {
                                @Override
                                public void onResponse(Call<ApiResponseBody> call, Response<ApiResponseBody> response) {
                                    if (response.isSuccessful()) {
                                        mBaseAdapter.removeData(position);
                                    } else {
                                        showTost(response.raw().message(), 1);
                                    }
                                }

                                @Override
                                public void onFailure(Call<ApiResponseBody> call, Throwable t) {
                                    ApiException apiException = ExceptionHandle.handleException(t);
                                    if (apiException.isTokenInvalid()) {
                                        showUnLoginSnackbar();
                                    } else if (apiException.isNetConnected()) {
                                        showSetNetworkSnackbar();
                                    } else {
                                        showTost(apiException.getMessage(), 0);
                                    }
                                }
                            });

                            break;
                        case VIEW_TYPE_RENT:
                            MyPublishVO.LeaseListBean bean1 = (MyPublishVO.LeaseListBean) mBaseAdapter.getData().get(position);
                            params.put("id", bean1.getIdX());
                            params.put("isDel", 1);
                            Call<ApiResponseBody> call1 = mApiservice.deleteRent(params);
                            call1.enqueue(new Callback<ApiResponseBody>() {
                                @Override
                                public void onResponse(Call<ApiResponseBody> call, Response<ApiResponseBody> response) {
                                    if (response.isSuccessful()) {
                                        mBaseAdapter.removeData(position);
                                    } else {
                                        showTost(response.raw().message(), 1);
                                    }
                                }

                                @Override
                                public void onFailure(Call<ApiResponseBody> call, Throwable t) {
                                    ApiException apiException = ExceptionHandle.handleException(t);
                                    if (apiException.isTokenInvalid()) {
                                        showUnLoginSnackbar();
                                    } else if (apiException.isNetConnected()) {
                                        showSetNetworkSnackbar();
                                    } else {
                                        showTost(apiException.getMessage(), 0);
                                    }
                                }
                            });
                            break;
                        case VIEW_TYPE_QIUZU:
                            MyPublishVO.QiuZuListBean bean2 = (MyPublishVO.QiuZuListBean) mBaseAdapter.getData().get(position);
                            params.put("id", bean2.getIdX());
                            params.put("isDel", 1);
                            Call<ApiResponseBody> call2 = mApiservice.subQiuZu(params);
                            call2.enqueue(new Callback<ApiResponseBody>() {
                                @Override
                                public void onResponse(Call<ApiResponseBody> call, Response<ApiResponseBody> response) {
                                    if (response.isSuccessful()) {
                                        mBaseAdapter.removeData(position);
                                    } else {
                                        showTost(response.raw().message(), 1);
                                    }
                                }

                                @Override
                                public void onFailure(Call<ApiResponseBody> call, Throwable t) {
                                    ApiException apiException = ExceptionHandle.handleException(t);
                                    if (apiException.isTokenInvalid()) {
                                        showUnLoginSnackbar();
                                    } else if (apiException.isNetConnected()) {
                                        showSetNetworkSnackbar();
                                    } else {
                                        showTost(apiException.getMessage(), 0);
                                    }
                                }
                            });
                            break;
                        case VIEW_TYPE_USED_CHECK:
                            MyPublishVO.TwoHardCheckBean bean3 = (MyPublishVO.TwoHardCheckBean) mBaseAdapter.getData().get(position);
                            params.put("id", bean3.getIdX());
                            params.put("userid", PreferencesUtil.getString(getActivity(), PreferencesUtil.USER_ID));
                            Call<ApiResponseBody> call3 = mApiservice.delCheck(params);
                            call3.enqueue(new Callback<ApiResponseBody>() {
                                @Override
                                public void onResponse(Call<ApiResponseBody> call, Response<ApiResponseBody> response) {
                                    if (response.isSuccessful()) {
                                        mBaseAdapter.removeData(position);
                                    } else {
                                        showTost(response.raw().message(), 1);
                                    }
                                }

                                @Override
                                public void onFailure(Call<ApiResponseBody> call, Throwable t) {
                                    ApiException apiException = ExceptionHandle.handleException(t);
                                    if (apiException.isTokenInvalid()) {
                                        showUnLoginSnackbar();
                                    } else if (apiException.isNetConnected()) {
                                        showSetNetworkSnackbar();
                                    } else {
                                        showTost(apiException.getMessage(), 0);
                                    }
                                }
                            });
                            break;
                        case VIEW_TYPE_HELP_CHECK:
                            MyPublishVO.GZCheckBean bean4 = (MyPublishVO.GZCheckBean) mBaseAdapter.getData().get(position);
                            params.put("id", bean4.getIdX());
                            Call<ApiResponseBody> call4 = mApiservice.delCheck(params);
                            call4.enqueue(new Callback<ApiResponseBody>() {
                                @Override
                                public void onResponse(Call<ApiResponseBody> call, Response<ApiResponseBody> response) {
                                    if (response.isSuccessful()) {
                                        mBaseAdapter.removeData(position);
                                    } else {
                                        showTost(response.raw().message(), 1);
                                    }
                                }

                                @Override
                                public void onFailure(Call<ApiResponseBody> call, Throwable t) {
                                    ApiException apiException = ExceptionHandle.handleException(t);
                                    if (apiException.isTokenInvalid()) {
                                        showUnLoginSnackbar();
                                    } else if (apiException.isNetConnected()) {
                                        showSetNetworkSnackbar();
                                    } else {
                                        showTost(apiException.getMessage(), 0);
                                    }
                                }
                            });
                            break;
                        case VIEW_TYPE_ASK:
                            MyPublishVO.ProblemListBean bean6 = (MyPublishVO.ProblemListBean) mBaseAdapter.getData().get(position);
                            //0:未解答 1:已解答
                            if (bean6.getIsAnswer() == 0) {
//                        mBaseAdapter.removeData(position);
                                params.put("id", bean6.getQId());
                                params.put("isDel", 1);
                                Call<ApiResponseBody> call6 = mApiservice.deleteAsk(params);
                                call6.enqueue(new Callback<ApiResponseBody>() {
                                    @Override
                                    public void onResponse(Call<ApiResponseBody> call, Response<ApiResponseBody> response) {
                                        if (response.isSuccessful()) {
                                            mBaseAdapter.removeData(position);
                                        } else {
                                            showTost(response.raw().message(), 1);
                                        }
                                    }

                                    @Override
                                    public void onFailure(Call<ApiResponseBody> call, Throwable t) {
                                        ApiException apiException = ExceptionHandle.handleException(t);
                                        if (apiException.isTokenInvalid()) {
                                            showUnLoginSnackbar();
                                        } else if (apiException.isNetConnected()) {
                                            showSetNetworkSnackbar();
                                        } else {
                                            showTost(apiException.getMessage(), 0);
                                        }
                                    }
                                });
                            } else {
                                showTost("已经回答的问题不能删除", 0);
                            }
                            break;
                    }
                } else if (direction == SwipeMenuRecyclerView.LEFT_DIRECTION) {
                }
            }
        };
        mBaseAdapter = new BaseAdapter(getActivity()) {
            @Override
            public BaseViewHolder getViewHolder(ViewGroup parent, int viewType) {
                BaseViewHolder holder = null;
                //二手机
                if (viewType == VIEW_TYPE_USED) {
                    holder = new BaseViewHolder(LayoutInflater.from(getActivity()).inflate(R.layout.item_my_publish_used, parent, false), new BaseViewHolder.Callbacks() {
                        @Override
                        public void bind(BaseViewHolder holder, Object data) {
                            if (data instanceof MyPublishVO.TwoHardListBean) {
                                MyPublishVO.TwoHardListBean bean = (MyPublishVO.TwoHardListBean) data;
                                ImageView img = holder.getView(R.id.img);
                                ImageDisplayUtil.LoadNetImageWithUrl(img, getActivity(), bean.getBanner_pic());
                                TextView name = holder.getView(R.id.tv_name);
                                holder.setText(R.id.tag, getString(R.string.homePage_used_machine));
                                name.setText(bean.getTitle());
                                if (bean.getState() == 0) {
                                    holder.setText(R.id.tv_state, "待售");
                                } else {
                                    holder.setText(R.id.tv_state, "已售");
                                }
                                DecimalFormat df = new DecimalFormat("0.00");
                                holder.setText(R.id.tv_price, String.valueOf(df.format(bean.getPrice())) + bean.getPrice_unit());
                            }

                        }

                        @Override
                        public void onItemClick(View view, int position) {
                            MyPublishVO.TwoHardListBean bean = (MyPublishVO.TwoHardListBean) mBaseAdapter.getData().get(position);
                            Intent intent = new Intent(getActivity(), EditUsedMachineActivity.class);
                            intent.putExtra("deviceid", bean.getIdX());
                            startActivity(intent);
                        }
                    });
                    //设备出租
                } else if (viewType == VIEW_TYPE_RENT) {
                    holder = new BaseViewHolder(LayoutInflater.from(getActivity()).inflate(R.layout.item_my_publish_used, parent, false), new BaseViewHolder.Callbacks() {
                        @Override
                        public void bind(BaseViewHolder holder, Object data) {
                            if (data instanceof MyPublishVO.LeaseListBean) {
                                MyPublishVO.LeaseListBean bean = (MyPublishVO.LeaseListBean) data;
                                ImageView img = holder.getView(R.id.img);
                                ImageDisplayUtil.LoadNetImageWithUrl(img, getActivity(), bean.getBanner_pic());
                                TextView name = holder.getView(R.id.tv_name);
                                holder.setText(R.id.tag, "设备出租");
                                name.setText(bean.getTitle());
                                if (bean.getState() == 0) {
                                    holder.setText(R.id.tv_state, "待租");
                                } else {
                                    holder.setText(R.id.tv_state, "忙碌");
                                }
                                holder.setText(R.id.tv_price, Utils.D2I(bean.getPrice()) + bean.getPrice_unit() + "");
                            }

                        }

                        @Override
                        public void onItemClick(View view, int position) {
                            MyPublishVO.LeaseListBean bean = (MyPublishVO.LeaseListBean) mBaseAdapter.getData().get(position);
                            Intent intent = new Intent(getActivity(), EditRentActicity.class);
                            intent.putExtra("deviceid", bean.getIdX());
                            startActivity(intent);
                        }
                    });
                } else if (viewType == VIEW_TYPE_QIUZU) {//设备求租
                    holder = new BaseViewHolder(LayoutInflater.from(getActivity()).inflate(R.layout.item_my_publish_used_qiuzu, parent, false), new BaseViewHolder.Callbacks() {
                        @Override
                        public void bind(BaseViewHolder holder, Object data) {
                            if (data instanceof MyPublishVO.QiuZuListBean) {
                                MyPublishVO.QiuZuListBean bean = (MyPublishVO.QiuZuListBean) data;
                                TextView name = holder.getView(R.id.tv_name);
                                name.setText(bean.getTitle());
                                holder.setText(R.id.tag, "设备求租");
                                holder.setText(R.id.tv_time, "工期" + bean.getTime_limit() + "天");
                                holder.setText(R.id.tv_price, String.valueOf(Utils.D2I(bean.getPrice())) + bean.getPrice_unit());
                            }
                        }

                        @Override
                        public void onItemClick(View view, int position) {
                            MyPublishVO.QiuZuListBean bean = (MyPublishVO.QiuZuListBean) mBaseAdapter.getData().get(position);
                            Intent intent = new Intent(getActivity(), EditQiuzuActicity.class);
                            intent.putExtra("deviceid", bean.getIdX());
                            startActivity(intent);
                        }
                    });
                } else if (viewType == VIEW_TYPE_USED_CHECK) {//二手机检测
                    holder = new BaseViewHolder(LayoutInflater.from(getActivity()).inflate(R.layout.item_my_publish_used_check, parent, false), new BaseViewHolder.Callbacks() {
                        @Override
                        public void bind(BaseViewHolder holder, Object data) {
                            if (data instanceof MyPublishVO.TwoHardCheckBean) {
                                MyPublishVO.TwoHardCheckBean bean = (MyPublishVO.TwoHardCheckBean) data;
                                holder.setText(R.id.tv_name, bean.getName());
                                holder.setText(R.id.tag, getString(R.string.homePage_used_check));
                                holder.setText(R.id.tv_time, Utils.getDateYMD(bean.getProduction_date())
                                        + " | " + bean.getPro_name().replaceAll("省", "") + " " + bean.getCity_name().replaceAll("市", ""));
                                //// TODO: 2018/2/27 到底是什么时间？ 应该是检测日期，而不是生产日期
                            }
                        }

                        @Override
                        public void onItemClick(View view, int position) {

                        }
                    });

                } else if (viewType == VIEW_TYPE_HELP_CHECK) {//故障代检
                    holder = new BaseViewHolder(LayoutInflater.from(getActivity()).inflate(R.layout.item_my_publish_help_check, parent, false), new BaseViewHolder.Callbacks() {
                        @Override
                        public void bind(BaseViewHolder holder, Object data) {
                            if (data instanceof MyPublishVO.GZCheckBean) {
                                MyPublishVO.GZCheckBean bean = (MyPublishVO.GZCheckBean) data;
                                holder.setText(R.id.tv_name, bean.getName());
                                holder.setText(R.id.tag, getString(R.string.homePage_problem_check));
                                holder.setText(R.id.tv_time, Utils.getDateYMD(bean.getProduction_date())
                                        + " | " + bean.getPro_name().replaceAll("省", "") + " " + bean.getCity_name().replaceAll("市", ""));
                                //// TODO: 2018/2/27 到底是什么时间？ 应该是检测日期，而不是生产日期
                            }
                        }

                        @Override
                        public void onItemClick(View view, int position) {

                        }
                    });


                } else if (viewType == VIEW_TYPE_ASK) {//问答
                    holder = new BaseViewHolder(LayoutInflater.from(getActivity()).inflate(R.layout.item_my_publish_ask, parent, false), new BaseViewHolder.Callbacks() {
                        @Override
                        public void bind(BaseViewHolder holder, Object data) {
                            if (data instanceof MyPublishVO.ProblemListBean) {
                                final MyPublishVO.ProblemListBean bean = (MyPublishVO.ProblemListBean) data;
                                TextView name = holder.getView(R.id.tv_question);
                                TextView noAnswer = holder.getView(R.id.tv_default);
                                RelativeLayout hasAnswer = holder.getView(R.id.rl_answer);
                                LinearLayout ll_skipToDetail = holder.getView(R.id.ll_skipToDetail);
                                name.setText(bean.getQContent());
                                if (bean.getIsAnswer() == 0) {
                                    hasAnswer.setVisibility(View.GONE);
                                    noAnswer.setVisibility(View.VISIBLE);
                                } else {
                                    hasAnswer.setVisibility(View.VISIBLE);
                                    noAnswer.setVisibility(View.GONE);
                                    ll_skipToDetail.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
//                                            SkipToEngineerDetail(getActivity(), String.valueOf(bean.getAId()));
                                        }
                                    });
                                    ImageView head = holder.getView(R.id.iv_answer_head);
                                    TextView mTag1 = holder.getView(R.id.tv_tag3);
                                    TextView mTag2 = holder.getView(R.id.tv_tag4);
                                    holder.setText(R.id.tv_zan, String.valueOf(bean.getLikeNumber()));
                                    holder.setText(R.id.tv_answer, "答: " + bean.getAContent());
                                    holder.setText(R.id.tv_name, bean.getaRealName());
                                    ImageDisplayUtil.LoadCircleImg(getActivity(), bean.getAUserPic(), head);
                                    String[] str = bean.getALabel().split(",");
                                    if (str.length == 0) {
                                        mTag1.setVisibility(View.GONE);
                                        mTag2.setVisibility(View.GONE);
                                    } else if (str.length == 1) {
                                        if (Util.isEmpty(str[0])) {
                                            mTag1.setVisibility(View.GONE);
                                        } else {
                                            mTag1.setVisibility(View.VISIBLE);
                                        }
                                        mTag2.setVisibility(View.GONE);
                                        mTag1.setText(str[0]);
                                    } else if (str.length >= 2) {
                                        mTag1.setVisibility(View.VISIBLE);
                                        mTag2.setVisibility(View.VISIBLE);
                                        mTag1.setText(str[0]);
                                        mTag2.setText(str[1]);
                                    }

                                }
                            }
                        }

                        @Override
                        public void onItemClick(View view, int position) {
                            MyPublishVO.ProblemListBean bean = (MyPublishVO.ProblemListBean) mBaseAdapter.getData().get(position);
                            FreeAskAnswerDetailActivity.startFreeAskAnswerDetailActivty( String.valueOf(bean.getQId()),getContext());

                        }
                    });

                }
                return holder;
            }


            @Override
            public int getItemViewType(int position) {
                Object bean = mBaseAdapter.getData().get(position);
                if (bean instanceof MyPublishVO.LeaseListBean) {
                    return VIEW_TYPE_RENT;
                } else if (bean instanceof MyPublishVO.TwoHardListBean) {
                    return VIEW_TYPE_USED;
                } else if (bean instanceof MyPublishVO.QiuZuListBean) {
                    return VIEW_TYPE_QIUZU;
                } else if (bean instanceof MyPublishVO.ProblemListBean) {
                    return VIEW_TYPE_ASK;
                } else if (bean instanceof MyPublishVO.TwoHardCheckBean) {
                    return VIEW_TYPE_USED_CHECK;
                } else if (bean instanceof MyPublishVO.GZCheckBean) {
                    return VIEW_TYPE_HELP_CHECK;
                } else
                    return super.getItemViewType(position);
            }
        };

        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        mRecyclerView.setSwipeMenuCreator(swipeMenuCreator);
        mRecyclerView.setSwipeMenuItemClickListener(mMenuItemClickListener);
        mRecyclerView.setAdapter(mBaseAdapter);


    }

    @Override
    public void loadData() {
        getData();
    }


    public void getData() {
        mApiservice = ApiUtil.getInstance().create();
        HashMap params = new HashMap();
        params.put("type", type);
        params.put("userid", PreferencesUtil.getString(getActivity(), PreferencesUtil.USER_ID));
        Call<ApiResponseBody<MyPublishVO>> call = mApiservice.mySubList(params);
        call.enqueue(new Callback<ApiResponseBody<MyPublishVO>>() {
            @Override
            public void onResponse(Call<ApiResponseBody<MyPublishVO>> call, Response<ApiResponseBody<MyPublishVO>> response) {
                if (response.isSuccessful()) {
                    MyPublishVO bean = response.body().getResult();
                    if (bean == null) {
                        visibleNoData();
                    } else {
                        mBaseAdapter.removeAllData();
                        if (bean.getTwoHardList() != null) {
                            mBaseAdapter.setData(bean.getTwoHardList());
                        }
                        if (bean.getLeaseList() != null)
                            mBaseAdapter.addData(BaseAdapter.AddType.LASE, bean.getLeaseList());
                        if (bean.getQiuZuList() != null)
                            mBaseAdapter.addData(BaseAdapter.AddType.LASE, bean.getQiuZuList());
                        if (bean.getTwoHardCheck() != null)
                            mBaseAdapter.addData(BaseAdapter.AddType.LASE, bean.getTwoHardCheck());
                        if (bean.getGZCheck() != null)
                            mBaseAdapter.addData(BaseAdapter.AddType.LASE, bean.getGZCheck());
                        if (bean.getProblemList() != null)
                            mBaseAdapter.addData(BaseAdapter.AddType.LASE, bean.getProblemList());
                    }
                    if (mBaseAdapter.getData().size() == 0) {
                        visibleNoData();
                    }
                } else {
                    showTost(response.raw().message(), 1);
                }
            }

            @Override
            public void onFailure(Call<ApiResponseBody<MyPublishVO>> call, Throwable t) {
                ApiException apiException = ExceptionHandle.handleException(t);
                if (apiException.isTokenInvalid()) {
                    showUnLoginSnackbar();
                } else if (apiException.isNetConnected()) {
                    showSetNetworkSnackbar();
                } else {
                    showTost(apiException.getMessage(), 0);
                }
            }
        });


    }


    private OnItemMovementListener mItemMovementListener = new OnItemMovementListener() {
        @Override
        public int onDragFlags(RecyclerView recyclerView, RecyclerView.ViewHolder targetViewHolder) {
            return 0;
        }

        @Override
        public int onSwipeFlags(RecyclerView recyclerView, RecyclerView.ViewHolder targetViewHolder) {
            int adapterPosition = targetViewHolder.getAdapterPosition();
            int viewtype = mBaseAdapter.getItemViewType(adapterPosition);
            if (viewtype == VIEW_TYPE_ASK) {
                MyPublishVO.ProblemListBean bean = (MyPublishVO.ProblemListBean) mBaseAdapter.getData().get(adapterPosition);
                if (bean.getIsAnswer() == 1) {
                    return OnItemMovementListener.INVALID;// 返回无效的方向。
                }
            }
            return OnItemMovementListener.LEFT | OnItemMovementListener.RIGHT;
        }
    };

    /**
     * 当Item移动的时候监听。
     */
    private OnItemMoveListener onItemMoveListener = new OnItemMoveListener() {
        @Override
        public boolean onItemMove(RecyclerView.ViewHolder srcHolder, RecyclerView.ViewHolder targetHolder) {
            return false;
        }


        @Override
        public void onItemDismiss(RecyclerView.ViewHolder srcHolder) {
            final int position = srcHolder.getAdapterPosition();
            int viewtype = mBaseAdapter.getItemViewType(position);

        }
    };


    @Override
    public void onResume() {
        super.onResume();
        getData();
    }
}
